﻿using System;
using System.Collections.Generic;
using System.Net;
using Cz;
using Cz.CzDbMySql;
using Cz.CzDbMySqlPool;
using Cz.CzDbSqlite;
using Cz.CzFileConfig;
using Cz.CzHttpWebService;
using Cz.CzJson;
using Cz.CzLog;
using Cz.CzPool;
using Cz.CzRpcReflection;

namespace Test
{
    class Program
    {
        public static ICzJson json;
        public static ICzPool pool;
        
        public static ICzLog log;
        public static ICzConfig config;
        
        public static ICzTask task;
        public static ICzHttp http;
        public static ICzDb mysql;
        
        public static ICzRpc rpc;
        
        static void Main(string[] args)
        {
            var factory = new CzFactory();

            json = factory.AddService(new CzJson2Dictionary());
            pool = factory.AddService(new CzPool());
            
            config = factory.AddService(new CzFileConfig(AppDomain.CurrentDomain.BaseDirectory + "config.json"));
            log = factory.AddService(new CzLog4Net());
            task = factory.AddService(new CzTask());
            
            rpc = factory.AddService(new CzRpcReflection(new HttpMethods()));
            
            //mysql = factory.AddService(new CzDbMySql());
            //mysql = factory.AddService(new CzDbMySqlPool());
            mysql = factory.AddService(new CzDbSqlite());
            
            http = factory.AddService(new CzHttpWebService());
            // http.handleRequest += HttpOnHandleRequest;
            http.handleRequest += HttpOnHandleRequest_Rpc;
            
            factory.Start();
            
            log.Info("服务器启动完成");
            
            while (true)
            {
                Console.ReadLine();
            }
        }

        private static void HttpOnHandleRequest(HttpListenerRequest request, HttpListenerResponse response)
        {
            log.Info("request.Url.Segments: " + request.Url.Segments.ToJson());
            
            response.ContentType = "text/html; charset=utf-8";
            string ret = "<pre>\n";
            
            ret += "ExecTable:" + mysql.ExecTable($"select * from gamepara limit 10").ToJson();
            
            // ret += "\n\nExecRow:" + mysql.ExecRow($"select * from gamepara where `key` = @p1",
            //     mysql.CreatePara("@p1", "addFightTime")).ToJson();
            //
            // ret += "\n\nExecString:\n" + mysql.ExecString($"select value from gamepara where `key` = @p1",
            //     mysql.CreatePara("@p1", "addFightTime"));
            
            ret += "\n</pre>";

            var bytes = System.Text.Encoding.UTF8.GetBytes(ret);
            response.OutputStream.Write(bytes, 0, bytes.Length);

            // log.Info("response");
            
            // log.Info("CzHttpWebService", $"[response]{request.Url}\n" +
            //                              $"======================begin=======================\n" +
            //                              $"{ret}\n" +
            //                              $"=======================end========================");
        }
        
        // RPC
        private static void HttpOnHandleRequest_Rpc(HttpListenerRequest request, HttpListenerResponse response)
        {
            string methodName = request.Url.Segments.Length < 2 ? "Default" : request.Url.Segments[1];
            var jsonStr = request.InputStream.ReadString();
            
            log.Info($"req:{request.Url}", jsonStr);
            
            var parameters = json.Deserialize<Dictionary<string, object>>(jsonStr);
            
            var ret = rpc.Dispatch(methodName, parameters);
            var retJson = json.Serialize(ret);
            
            response.ContentType = "application/json; charset=utf-8";
            var bytes = System.Text.Encoding.UTF8.GetBytes(retJson);
            response.OutputStream.Write(bytes, 0, bytes.Length);
            
            log.Info($"response:{request.Url}", retJson);
        }
    }
}